package dj.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class BeanExtendUtils {

	public static void setRequestToBean(Object bean, HttpServletRequest request) throws Exception {
		BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
		PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

		for (int j = 0; j < pds.length; j++) {
			Method smethod = pds[j].getWriteMethod();
			String name = pds[j].getName();

			if ("class".equals(name)) {
				continue;
			}

			String columnValue = request.getParameter(name);
			String returnType = smethod.getParameterTypes()[0].getName();
			if ("".equals(StringUtils.isNull(columnValue))) {
				continue;
			}
			Object object;
			if (returnType.equals("int")) {
				object = new Integer(columnValue);
			} else if (returnType.equals("long")) {
				object = new Long(columnValue);
			} else if (returnType.equals("boolean")) {
				object = new Boolean(columnValue);
			} else if (returnType.equals("[C")) {
				continue;
			} else if (returnType.equals("java.util.Date")) {
				object = DateUtils.toDate(columnValue);
			} else if (returnType.equals("float")) {
				object = new Float(columnValue);
			} else if (returnType.equals("double")) {
				object = new Double(columnValue);
			} else {
				object = columnValue;
			}
			smethod.invoke(bean, new Object[] { object });
		}
	}

	/**
	 * 吧bean 转换成map key为变量名
	 * 
	 * @param bean
	 * @return
	 */
	public static Map<String, Object> beanToMap(Object bean) {

		Map<String, Object> retMap = new HashMap<String, Object>();

		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
			PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

			for (int j = 0; j < pds.length; j++) {
				Method smethod = pds[j].getReadMethod();
				String name = pds[j].getName();

				if ("class".equals(name)) {
					continue;
				}

				Object object = smethod.invoke(bean);

				retMap.put(name, object);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retMap;
	}

	/**
	 * 把bean的list 转换成map形式的list
	 * @param beans
	 * @return
	 */
	public static List<Map<String, Object>> beansToMaps(List beans) {

		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();

		try {

			for (Object bean : beans) {
				Map<String, Object> retMap = new HashMap<String, Object>();

				BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
				PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

				for (int j = 0; j < pds.length; j++) {
					Method smethod = pds[j].getReadMethod();
					String name = pds[j].getName();

					if ("class".equals(name)) {
						continue;
					}

					Object object = smethod.invoke(bean);

					retMap.put(name, object);
				}
				maps.add(retMap);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return maps;
	}

	/**
	 * 把map的值 赋给指定的bean
	 * 
	 * @param clazz
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static Object mapToBean(Class<?> clazz, Map map) throws Exception {
		Object bean = null;
		try {

			Class<?> classType = clazz.getClass();
			bean = classType.getConstructor(new Class[] {}).newInstance(new Object[] {}); // new 一个bean

			BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
			PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

			for (int j = 0; j < pds.length; j++) {

				Method smethod = pds[j].getWriteMethod();
				String name = pds[j].getName();
				if (null != name) {
					if (null != map.get(name)) {
						String returnType = smethod.getParameterTypes()[0].getName();
						Object object = null;
						if (returnType.equals("int"))
							object = (Integer) map.get(name);
						else if (returnType.equals("long"))
							object = (Long) map.get(name);
						else if (returnType.equals("boolean"))
							object = (Boolean) map.get(name);
						else if (returnType.equals("float"))
							object = new Float(map.get(name).toString());
						else if (returnType.equals("double"))
							object = new Double(map.get(name).toString());
						else
							object = map.get(name);

						smethod.invoke(bean, new Object[] { object });
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return bean;
	}

	/**
	 * 根据map的key 给bean 赋值 ， 返回一个bean的list
	 * 
	 * @param clazz
	 * @param ls
	 * @return
	 * @throws Exception
	 */
	public static List mapsToBeans(Class<?> clazz, List<Map<String, Object>> ls) throws Exception {
		List<Object> values = new ArrayList<Object>();
		try {

			if (null != ls && ls.size() > 0) {

				for (Map<String, Object> valueMap : ls) {

					Class<?> classType = clazz.getClass();
					Object bean = classType.getConstructor(new Class[] {}).newInstance(new Object[] {}); // new 一个bean

					BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
					PropertyDescriptor pds[] = beanInfo.getPropertyDescriptors();

					for (int j = 0; j < pds.length; j++) {

						Method smethod = pds[j].getWriteMethod();
						String name = pds[j].getName();
						if (null != name) {
							if (null != valueMap.get(name)) {
								String returnType = smethod.getParameterTypes()[0].getName();
								Object object = null;
								if (returnType.equals("int"))
									object = (Integer) valueMap.get(name);
								else if (returnType.equals("long"))
									object = (Long) valueMap.get(name);
								else if (returnType.equals("boolean"))
									object = (Boolean) valueMap.get(name);
								else if (returnType.equals("float"))
									object = new Float(valueMap.get(name).toString());
								else if (returnType.equals("double"))
									object = new Double(valueMap.get(name).toString());
								else
									object = valueMap.get(name);

								smethod.invoke(bean, new Object[] { object });
							}
						}
					}

					values.add(bean);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return values;
	}
}
